package chapter5.java1;

/**
 * @Author juzipi
 * @Date 2021/5/18 10:17
 * @Info
 */
public class StringBuilderTest {

    //method1里的stringBuilder声明方式是线程安全的
    public static void method1(){
        //stringBuilder:线程不安全
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("a");
        stringBuilder.append("b");
    }

    //线程不安全的
    public static void method2(StringBuilder stringBuilder){
        stringBuilder.append("a");
        stringBuilder.append("b");
    }

    /*
    内部产生，内部消亡的一般都是安全的
    外部产生，未消亡的都是线程不安全
     */
    //返回的stringBuilder是线程不安全的
    public static StringBuilder method3(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("a");
        stringBuilder.append("b");
        return stringBuilder;
    }

    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        new Thread(() -> {
            stringBuilder.append("a");
            stringBuilder.append("b");
        }).start();

        method2(stringBuilder);

    }

}
